package com.wm.blog_common.base;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;

/***
 * @ClassName: BeanUtils
 * @Description:
 * @Author: wm_yu
 * @Create_time: 11:10 2020-3-4
 */
public class BeanUtils {
    private static final Logger log = LoggerFactory.getLogger(BeanUtils.class);

    private BeanUtils() {
    }

    public static <T> T copyProperties(Object source, Class<T> targetClass) {
        if (source == null) {
            return null;
        } else {
            Object target = null;

            try {
                if (Number.class.isAssignableFrom(targetClass)) {
                    return (T) source;
                }

                target = targetClass.newInstance();
                org.springframework.beans.BeanUtils.copyProperties(source, target);
            } catch (Exception var4) {
                log.error("{}", var4);
            }

            return (T) target;
        }
    }

    public static <T> T deepCopyProperties(Object source, Class<T> targetClass) {
        return source == null ? null : JSONObject.parseObject(JSONObject.toJSONString(source), targetClass);
    }


    public static <T> T copyProperties(Object source, T target) {
        if (source == null) {
            return null;
        } else {
            try {
                if (Number.class.isAssignableFrom(target.getClass())) {
                    return (T) source;
                }

                org.springframework.beans.BeanUtils.copyProperties(source, target);
            } catch (Exception var3) {
                log.error("{}", var3);
            }

            return target;
        }
    }

    public static <T> List<T> copyListProperties(List<?> sourceList, Class<T> targetClass) {
        if (CollectionUtils.isEmpty(sourceList)) {
            return null;
        } else {
            ArrayList<T> ts = new ArrayList(sourceList.size());
            sourceList.forEach((item) -> {
                ts.add(copyProperties(item, targetClass));
            });
            return ts;
        }
    }

    public static <T> List<T> deepCopyListProperties(List<?> sourceList, Class<T> targetClass) {
        return CollectionUtils.isEmpty(sourceList) ? null : JSONArray.parseArray(JSONObject.toJSONString(sourceList), targetClass);
    }


    /**
     * 将对象转换为map
     *
     * @param obj
     * @return
     */
    public static Map<String, Object> object2Map(Object obj, boolean... ignoreNull) {
        Map<String, Object> map = new HashMap();
        if (obj == null) {
            return map;
        } else {
            Class clazz = obj.getClass();
            Field[] fields = clazz.getDeclaredFields();
            boolean notNull = false;
            if (!ObjectUtils.isEmpty(ignoreNull) && ignoreNull[0]) {
                notNull = ignoreNull[0];
            }
            try {
                Field[] var4 = fields;
                int var5 = fields.length;
                for (int var6 = 0; var6 < var5; ++var6) {
                    Field field = var4[var6];
                    field.setAccessible(true);
                    Object o = field.get(obj);
                    if (notNull && ObjectUtils.isEmpty(o)) {
                        continue;
                    } else {
                        map.put(field.getName(), field.get(obj));
                    }
                }
                return map;
            } catch (Exception var8) {
                throw new RuntimeException(var8);
            }
        }
    }


    /**
     * 将map转换为对象,必须保证属性名称相同
     *
     * @return
     */
    public static <T> T map2Object(Map<String, Object> map, Class<T> clzz) {
        try {
            T target = clzz.newInstance();
            if (CollectionUtils.isEmpty(map)) {
                return target;
            }
            Field[] fields = clzz.getDeclaredFields();
            if (!CollectionUtils.isEmpty(Arrays.asList(fields))) {
                Arrays.stream(fields).filter((Field field) -> map.containsKey(field.getName())).forEach(var -> {
                    //获取属性的修饰符
                    int modifiers = var.getModifiers();
                    if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers)) {
                        //在lambada中结束本次循环是用return,它不支持continue和break
                        return;
                    }
                    //设置权限
                    var.setAccessible(true);
                    try {
                        var.set(target, map.get(var.getName()));
                    } catch (IllegalAccessException e) {
                        //属性类型不对,非法操作,跳过本次循环,直接进入下一次循环
                        throw new RuntimeException(e);
                    }
                });
            }
            return target;
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

}
